home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / ACGIFREE.ZIP / SAMPLES / FORM_EX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-23  |  3.8 KB  |  126 lines

  1. ////////////////////////////////////////
  2. //
  3. //  Sample CGI program for process FORM items
  4. //
  5. //    Call it like: http://www.myserver.net/mycgi/path cgi_ex.exe?MSG=Hello&LOC=World
  6. //      the extension can be .cgi, .exe or whatever, depending on your server      
  7. //
  8. //    You can also create an HTML page with a FORM submission and use this to test
  9. //      what you are supposed to receive.
  10. //
  11.  
  12. #define _DEBUG_DUMP_          //a_Enable for extended output (non-shareware version)
  13. //#define _WIN32_TEST_          //a_Enable for local debugging
  14.  
  15. #define MY_INPUT  "MY_INPUT"
  16. #define MY_SUBMIT "MY_SUBMIT"
  17.  
  18. #include "a_acgi.h"
  19.  
  20. int main()
  21. {
  22.  
  23.   #ifdef _WIN32_TEST_
  24.     char *pcTest=aStrDup("MY_INPUT=This%20is%20a%20test&MY_SUBMIT=Submit");
  25.   #endif
  26.  
  27.   ACGI cgiOut;
  28.  
  29.   //a_Get all possible form items usinf POST, GET or both
  30.   #ifdef _WIN32_TEST_
  31.     cgiOut.cgiGetFormItems(pcTest);
  32.   #else
  33.     cgiOut.cgiGetFormItems();
  34.   #endif
  35.  
  36.   //a_Start the HTML page
  37.   cgiOut.mimeHTML();                      //a_First ever thing to a browser is a MIME directive
  38.   cgiOut.htmlStartHTML();                 //a_Start the HTML page
  39.   cgiOut.htmlDoHEAD("Sample CGI form");   //a_Do the HEAD and TITLE in one
  40.   
  41.   //a_Start the <BODY>
  42.   cgiOut.htmlStartBODY();
  43.  
  44.   if (cgiOut.plGetValueByName(MY_SUBMIT))
  45.   {
  46.     //a_Reverse the string (yep, this is the heavy duty processing we need to do :)
  47.     const char *pccValue = cgiOut.plGetValueByName(MY_INPUT);
  48.     if (pccValue)
  49.     {
  50.       char *pcWork = aStrDup(pccValue);
  51.       if (pcWork)
  52.       { 
  53.         pcWork = strrev(pcWork);
  54.         cout << "Your sting reversed: <B><BIG>" << pcWork << "</B></BIG><BR>" << endl;
  55.       }
  56.       delete []pcWork;
  57.     }
  58.     else
  59.     {
  60.       cout << "<B>Your item was not found.</B><BR>" << endl;
  61.     }
  62.     
  63.     //a_There was a form submission, process input
  64.     cgiOut.htmlStartTag("H1", "ALIGN=CENTER");
  65.     cgiOut << "FORM Submission Items";
  66.     cgiOut.htmlEndTag("H1");
  67.  
  68.     //a_Form items... doOut is implemented in APairList
  69.     //a_doOut accepts AStreamOutput which is a grand-parent of ACGI,
  70.     //a_so as it turns out it works on itself...
  71.     //a_The output is formatted just like a list of CGI parameters.
  72.     //
  73.     //a_Ok, here is a silly example of what this means in real life,
  74.     //a_You are using something that you learned from your mother and applying
  75.     //a_it to something learned from your father...
  76.     //a_Sort of like sewing up a camping tent?!? :)
  77.     cgiOut << "<P><H2>As submitted</H2><BR>";
  78.     cgiOut.doOut(&cgiOut);
  79.     cgiOut.htmlStartTag("HR");
  80.     cgiOut.htmlStartTag("BR");
  81.   
  82.     //a_Now use the methods in ACGI inherited from both AHTML and AFormList
  83.     //a_to display the list contents
  84.     cgiOut << "<P><H2>After parsing</H2><BR>";
  85.     if (cgiOut.lGetCount() > 0x0)
  86.     {
  87.       cgiOut.htmlStartTag("PRE");
  88.       cgiOut.doPairs(&cgiOut, ' ', 0x1, 0x0, 0x0); //a_Unquoted, 1 per line, URL unencoded
  89.       cgiOut.htmlEndTag("PRE");
  90.     }
  91.     else
  92.       cgiOut << "No FORM items were submitted.<BR>" << endl;
  93.  
  94.     //a_Single tags can use Start for <tag>, without ever calling the end which does </tag>...
  95.     cgiOut.htmlStartTag("HR");
  96.     cgiOut.htmlStartTag("BR");
  97.   
  98.     //a_Dump the environment
  99.     cgiOut.cgiEnvironmentDump();
  100.   }
  101.   else
  102.   {
  103.     //a_Initial startup, show FORM items
  104.     cgiOut.cgiStartFORM();                  //a_Start FORM tag with ACTION to itself
  105.   
  106.     //a_Input item
  107.     AElementPairList eplInput;
  108.     
  109.     eplInput.plAddItem("NAME", MY_INPUT);
  110.     cgiOut.cgiDoFORMInput(FORMINPUT_TEXT, eplInput);
  111.  
  112.     cgiOut.cgiDoFORMInput(FORMINPUT_SUBMIT, MY_SUBMIT, "Submit");
  113.  
  114.     cgiOut.cgiEndFORM();                    //a_End the tag
  115.   }
  116.   
  117.   //a_The End...
  118.   cgiOut.htmlEndBODY();
  119.   cgiOut.htmlEndHTML();
  120.  
  121.   #ifdef _WIN32_TEST_
  122.     delete []pcTest;
  123.   #endif
  124.  
  125.   return 1;
  126. }